package br.com.sistema.smallAgro.util;

import java.io.Serializable;
import java.util.Iterator;

import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

import br.com.sistema.smallAgro.base.IEntidade;

public class ProviderBase<T extends IEntidade> extends ProviderGenerico<T> {
	private static final long serialVersionUID = -4373553811301381757L;

	private Serializable filtro;
	private AbstractPaginacaoService<T> service;

	public ProviderBase(Serializable filtro, AbstractPaginacaoService<T> service) {
		this.filtro = filtro;
		this.service = service;
	}

	@Override
	public void setFiltro(Serializable filtro) {
		this.filtro = filtro;
	}

	@Override
	public long size() {
		return service.contadorPaginado(filtro);
	}

	@Override
	public IModel<T> model(T object) {
		return new CompoundPropertyModel<T>(object);
	}

	@Override
	public void detach() {

	}

	@SuppressWarnings("unchecked")
	@Override
	public Iterator<? extends T> iterator(long first, long count) {
		return (Iterator<? extends T>) service.consultaPaginada(filtro, first, count);
	}

}
